<link href="Style.css" rel="stylesheet" type="text/css" />
<?php
session_start(); 

require("sql_functions.php");

if(isset($_SESSION['user']))
{

	$user = $_SESSION['user'];

  if( isset($_POST['formDate']) && isset($_POST['formHours']) && isset($_POST['projectID']))
  {
    $pid = $_POST['projectID'];
    $tName = $_POST['taskName'];
    $mName = $_POST['MilestoneName'];
    $date = makeSQLDate($_POST['formDate']);
    $hours = $_POST['formHours'];
    
    $currentHours = sql_getTaskHoursByDate($pid, $mName, $tName, $date);
    
    if($currentHours + $hours > 8)
    {
      header("Location: time_tracking.php?projectID=$pid&MilestoneName=$mName&taskName=$tName&error=1");
    }
  
    sql_createTimeTrack($pid, $mName, $tName, $date, $hours);
    header('Location:dashboard.php');
  }
  
	else if( isset($_GET['projectID']) && isset($_GET['MilestoneName']) && isset($_GET['taskName']) )
	{		
    if($_GET['error'])
    {
      echo "There was an error.  Enter at most 8 hours per day.";
    }
    
    $ttDate = makePHPDate("now");
  
		$pid = $_GET['projectID'];
		$mName = $_GET['MilestoneName'];
		$tName = $_GET['taskName'];
    
	  $task = sql_getTask($pid, $mName, $tName);
    
    $mName = $task['MilestoneName'];
    $tDesc = $task['Description'];
    $email = $task['EmployeeEmail'];
	
    $emp = sql_getEmployee($email);
    
    $fName = $emp['FirstName'];
    $lName = $emp['LastName'];
    
    $name = "$fName $lName";
	
	  echo "<html>"; 
	  echo "<head>"; 
	  echo "</head>"; 
	  echo "<body>";
	  echo "<p>Time Tracking"; 
	  echo "<p>Milestone: $mName";
	  echo "<p>Task: $tName";
	  echo "<p>Description: $tDesc";
	  echo "<p>Employee: $name";
	  echo "<form action=\"\" method=\"POST\">"; 
	  echo "<p>Date:";  
	  echo "<input name=\"formDate\" value=\"$ttDate\" />"; 
	  echo "<p>Hours:";  
	  echo "<input name=\"formHours\" value=\"$ttHours\" />"; 
	  echo "</p>";
	  echo "<input type=\"hidden\" name=\"projectID\" value=\"$pid\">";
    echo "<input type=\"hidden\" name=\"taskName\" value=\"$tName\">";
    echo "<input type=\"hidden\" name=\"MilestoneName\" value=\"$mName\">";
	  echo "<p><input type=\"submit\" name=\"save\" value=\"Save\" />"; 
	  echo "</form>";    
	  echo "</body>"; 
	  echo "</html>"; 
  }

	else
	{
		echo "Invalid Task";
	}

}
else
{
	echo "Must be logged in to view this page.";
}

?>
